const fs = require('fs');


function getBlockConfig(url,id) {
  const content = fs.readFileSync(url, 'utf-8');
  const schemaRegExpStr = /{% schema %}([\s\S]*?){% endschema %}/g;
  const schemBody = content.match(schemaRegExpStr);
  const item = JSON.parse(schemBody[0].replace(/({% schema %})|({% endschema %})/g, ''));
  item['block_type'] = id.replace('.liquid','');
  return item;
}


module.exports = function (engine) {
  const themesFlie = fs.readdirSync('./src/theme');
  const blacklist = ['.DS_Store', '.git', '.gitignore', '.vscode', 'system'];
  return themesFlie.filter((item) => {
    return !blacklist.includes(item) && item.indexOf('customtemp') === -1;
  }).map((file)=>{

    const sections = fs.readdirSync(`./src/theme/${file}/sections`);

    // const item = JSON.parse(fs.readFileSync(`./src/theme/${file}/config/settings_schema.json`, "utf-8"));
    // item['tempName'] = file
    // item['blocks'] = sections.map(section=>getBlockConfig(`./src/theme/${file}/sections/${section}`))

    return {
      tempName: file,
      blocks: sections.map(section=>getBlockConfig(`./src/theme/${file}/sections/${section}`,section)),
    };
  });
};
